﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using Microsoft.Win32;
using System.Diagnostics;
using iTextSharp;
using iTextSharp.text.pdf;
using System.Data;
using System.Windows.Controls;
using OutlookCalendar.Model;

namespace WpfApplication1
{
    class Pdf
    {
        public static int pdfPages = 0;
        public static int currPdfPages = 0;
        public static int currPrintPdfPages = 0;
        public static string wordtempname = "";
        public static string exceltempname = "";
        public static string pdftempname = "";
        public static List<DelegateObj> lsd = new List<DelegateObj>();
        public static string AppPath
        {
            get
            {
                return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6);
            }
        }

        public class DelegateObj
        {
            public string nr { get; set; }
            public string name { get; set; }
            public string type { get; set; }
        }

        public static void printToPDFPrograma(string name, DateTime currDate)
        {
            DateTime fd = Functionality.GetFirstDateOfWeek(currDate, DayOfWeek.Monday);
            DateTime ld = fd + TimeSpan.FromDays(6);

            iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4);
            PdfWriter.GetInstance(doc, new FileStream(name, FileMode.Create));

            doc.Open();

            BaseFont bF = BaseFont.CreateFont("c:\\windows\\fonts\\arial.ttf", "windows-1257", true);
            iTextSharp.text.Font fS = new iTextSharp.text.Font(bF, 10f, iTextSharp.text.Font.BOLD);
            iTextSharp.text.Font f = new iTextSharp.text.Font(bF, 12f, iTextSharp.text.Font.NORMAL);
            iTextSharp.text.Font fL = new iTextSharp.text.Font(bF, 20f, iTextSharp.text.Font.BOLD);
            iTextSharp.text.Font fb = new iTextSharp.text.Font(bF, 12f, iTextSharp.text.Font.BOLD);
            iTextSharp.text.Font fLT = new iTextSharp.text.Font(bF, 15f, iTextSharp.text.Font.NORMAL);
            iTextSharp.text.Font fm = new iTextSharp.text.Font(bF, 10f, iTextSharp.text.Font.NORMAL);
            iTextSharp.text.Font fLTB = new iTextSharp.text.Font(bF, 16f, iTextSharp.text.Font.UNDERLINE);


            try
            {
                string path = System.IO.Path.GetFullPath(AppPath + "\\Lrytas_tv.png");
                iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(path);
                logo.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
                logo.ScaleToFit(60, 200);
                doc.Add(logo);
            }
            catch (Exception ex) { }

            iTextSharp.text.Paragraph pp = new iTextSharp.text.Paragraph();
            pp.Alignment = iTextSharp.text.Element.ALIGN_LEFT;

            iTextSharp.text.Paragraph p1 = new iTextSharp.text.Paragraph();
            p1.Alignment = iTextSharp.text.Element.ALIGN_LEFT;

            iTextSharp.text.Chunk c1 = new iTextSharp.text.Chunk();
            c1.Font = f;
            c1.Append(" ");
            p1.Add(c1);

            doc.Add(p1);

            iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph();
            p.Alignment = iTextSharp.text.Element.ALIGN_CENTER;


            iTextSharp.text.Chunk c = new iTextSharp.text.Chunk();
            c.Font = fL;
            c.Append("Programa " + fd.ToShortDateString() + " - " + ld.ToShortDateString());
            p.Add(c);
            doc.Add(p);

            doc.Add(p1);
            

          
           Functionality.LoadAppointmentsByDate(fd, ld);
          
           bool[] labelday = new bool[7]{false,false,false,false,false,false,false};
           foreach (Appointment ap in Functionality.appointments)
           {
               if (ap.StartTime.ToShortDateString().Equals(fd.ToShortDateString()) && !labelday[0])
               {
                   doc.Add(p1);
                   iTextSharp.text.Paragraph pil = new iTextSharp.text.Paragraph();
                   pil.Alignment = iTextSharp.text.Element.ALIGN_LEFT;


                   iTextSharp.text.Chunk cil = new iTextSharp.text.Chunk();
                   cil.Font = fLTB;
                   cil.Append(fd.ToShortDateString().Replace('.', ' ') + " Pirmadienis");
                   pil.Add(cil);
                   doc.Add(pil);
                   doc.Add(p1);
                   labelday[0] = true;
               }
               if (ap.StartTime.ToShortDateString().Equals((fd + TimeSpan.FromDays(1)).ToShortDateString()) && !labelday[1])
               {
                   doc.Add(p1);
                   iTextSharp.text.Paragraph pil = new iTextSharp.text.Paragraph();
                   pil.Alignment = iTextSharp.text.Element.ALIGN_LEFT;


                   iTextSharp.text.Chunk cil = new iTextSharp.text.Chunk();
                   cil.Font = fLTB;
                   cil.Append(fd.ToShortDateString().Replace('.', ' ') + " Antradienis");
                   pil.Add(cil);
                   doc.Add(pil);
                   doc.Add(p1);
                   labelday[1] = true;
               }
               if (ap.StartTime.ToShortDateString().Equals((fd + TimeSpan.FromDays(2)).ToShortDateString()) && !labelday[2])
               {
                   doc.Add(p1);
                   iTextSharp.text.Paragraph pil = new iTextSharp.text.Paragraph();
                   pil.Alignment = iTextSharp.text.Element.ALIGN_LEFT;


                   iTextSharp.text.Chunk cil = new iTextSharp.text.Chunk();
                   cil.Font = fLTB;
                   cil.Append(fd.ToShortDateString().Replace('.', ' ') + " Trečiadienis");
                   pil.Add(cil);
                   doc.Add(pil);
                   doc.Add(p1);
                   labelday[2] = true;
               }
               if (ap.StartTime.ToShortDateString().Equals((fd + TimeSpan.FromDays(3)).ToShortDateString()) && !labelday[3])
               {
                   doc.Add(p1);
                   iTextSharp.text.Paragraph pil = new iTextSharp.text.Paragraph();
                   pil.Alignment = iTextSharp.text.Element.ALIGN_LEFT;


                   iTextSharp.text.Chunk cil = new iTextSharp.text.Chunk();
                   cil.Font = fLTB;
                   cil.Append(fd.ToShortDateString().Replace('.', ' ') + " Ketvirtadienis");
                   pil.Add(cil);
                   doc.Add(pil);
                   doc.Add(p1);
                   labelday[3] = true;
               }
               if (ap.StartTime.ToShortDateString().Equals((fd + TimeSpan.FromDays(4)).ToShortDateString()) && !labelday[4])
               {
                   doc.Add(p1);
                   iTextSharp.text.Paragraph pil = new iTextSharp.text.Paragraph();
                   pil.Alignment = iTextSharp.text.Element.ALIGN_LEFT;


                   iTextSharp.text.Chunk cil = new iTextSharp.text.Chunk();
                   cil.Font = fLTB;
                   cil.Append(fd.ToShortDateString().Replace('.', ' ') + " Penktadienis");
                   pil.Add(cil);
                   doc.Add(pil);
                   doc.Add(p1);
                   labelday[4] = true;
               }
               if (ap.StartTime.ToShortDateString().Equals((fd + TimeSpan.FromDays(5)).ToShortDateString()) && !labelday[5])
               {
                   doc.Add(p1);
                   iTextSharp.text.Paragraph pil = new iTextSharp.text.Paragraph();
                   pil.Alignment = iTextSharp.text.Element.ALIGN_LEFT;


                   iTextSharp.text.Chunk cil = new iTextSharp.text.Chunk();
                   cil.Font = fLTB;
                   cil.Append(fd.ToShortDateString().Replace('.', ' ') + " Šeštadienis");
                   pil.Add(cil);
                   doc.Add(pil);
                   doc.Add(p1);
                   labelday[5] = true;
               }
               if (ap.StartTime.ToShortDateString().Equals((fd + TimeSpan.FromDays(6)).ToShortDateString()) && !labelday[6])
               {
                   doc.Add(p1);
                   iTextSharp.text.Paragraph pil = new iTextSharp.text.Paragraph();
                   pil.Alignment = iTextSharp.text.Element.ALIGN_LEFT;


                   iTextSharp.text.Chunk cil = new iTextSharp.text.Chunk();
                   cil.Font = fLTB;
                   cil.Append(fd.ToShortDateString().Replace('.', ' ') + " Sekmadienis");
                   pil.Add(cil);
                   doc.Add(pil);
                   doc.Add(p1);
                   labelday[6] = true;
               }
               iTextSharp.text.Paragraph pi = new iTextSharp.text.Paragraph();
               pi.Alignment = iTextSharp.text.Element.ALIGN_LEFT;


               iTextSharp.text.Chunk ci = new iTextSharp.text.Chunk();
               ci.Font = f;
               string dataSubject=Functionality.getProgramDateFromDB(ap.Location,ap.Subject);
               ci.Append(ap.StartTime.ToShortTimeString().Substring(0, 5).Replace(':', '.') + " " + dataSubject);
               pi.Add(ci);
               doc.Add(pi);
               doc.Add(p1);
           }
           doc.Close();
        }

        public static void printToPDF(string name,DataGrid dt)
        {

            iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A0.Rotate());
            PdfWriter.GetInstance(doc, new FileStream(name, FileMode.Create));

            doc.Open();

            BaseFont bF = BaseFont.CreateFont("c:\\windows\\fonts\\arial.ttf", "windows-1257", true);
            iTextSharp.text.Font fS = new iTextSharp.text.Font(bF, 10f, iTextSharp.text.Font.BOLD);
            iTextSharp.text.Font f = new iTextSharp.text.Font(bF, 12f, iTextSharp.text.Font.NORMAL);
            iTextSharp.text.Font fL = new iTextSharp.text.Font(bF, 20f, iTextSharp.text.Font.BOLD);
            iTextSharp.text.Font fb = new iTextSharp.text.Font(bF, 12f, iTextSharp.text.Font.BOLD);
            iTextSharp.text.Font fLT = new iTextSharp.text.Font(bF, 15f, iTextSharp.text.Font.NORMAL);
            iTextSharp.text.Font fm = new iTextSharp.text.Font(bF, 10f, iTextSharp.text.Font.NORMAL);
            iTextSharp.text.Font fLTB = new iTextSharp.text.Font(bF, 16f, iTextSharp.text.Font.BOLD);

          
                try
                {
                    string path = System.IO.Path.GetFullPath(AppPath + "\\Lrytas_tv.png");
                    iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(path);
                    logo.Alignment = iTextSharp.text.Element.ALIGN_CENTER;
                    logo.ScaleToFit(60, 200);
                    doc.Add(logo);
                }
                catch (Exception ex) { }
  
           

                iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph();
                p.Alignment = iTextSharp.text.Element.ALIGN_CENTER;

                iTextSharp.text.Chunk c = new iTextSharp.text.Chunk();
                c.Font = fL;
                c.Append("Dumenų exportas");
                p.Add(c);
                doc.Add(p);
            
                        

                    iTextSharp.text.Paragraph pp = new iTextSharp.text.Paragraph();
                    pp.Alignment = iTextSharp.text.Element.ALIGN_LEFT;

                    iTextSharp.text.Paragraph p1 = new iTextSharp.text.Paragraph();
                    p1.Alignment = iTextSharp.text.Element.ALIGN_LEFT;

                    iTextSharp.text.Chunk c1 = new iTextSharp.text.Chunk();
                    c1.Font = f;
                    c1.Append(" ");
                    p1.Add(c1);

                    //----------------header----------
                    doc.Add(p1);
                    iTextSharp.text.Paragraph p2 = new iTextSharp.text.Paragraph();
                    p2.Alignment = iTextSharp.text.Element.ALIGN_CENTER;

                    PdfPTable table = new PdfPTable(40);
                   // table.DefaultCell.Border = 0;
                    table.WidthPercentage = 100f;

                 
                    PdfPCell cell1 = new PdfPCell(new iTextSharp.text.Phrase("Kasetes Nr.", fm));
                    PdfPCell cell2 = new PdfPCell(new iTextSharp.text.Phrase("Kompanija", fm));
                    PdfPCell cell3 = new PdfPCell(new iTextSharp.text.Phrase("Pavadinimas originalo kalba", fm));
                    PdfPCell cell4 = new PdfPCell(new iTextSharp.text.Phrase("Lietuviškas pavadinimas", fm));
                    PdfPCell cell5 = new PdfPCell(new iTextSharp.text.Phrase("Trukmė 1", fm));
                    PdfPCell cell6 = new PdfPCell(new iTextSharp.text.Phrase("Trukmė 2", fm));
                    PdfPCell cell7 = new PdfPCell(new iTextSharp.text.Phrase("TC trukmė", fm));
                    PdfPCell cell8 = new PdfPCell(new iTextSharp.text.Phrase("Pradzios TC", fm));
                    PdfPCell cell9 = new PdfPCell(new iTextSharp.text.Phrase("I Įsikirtimas", fm));
                    PdfPCell cell10 = new PdfPCell(new iTextSharp.text.Phrase("II Įsikirtimas", fm));
                    PdfPCell cell11 = new PdfPCell(new iTextSharp.text.Phrase("III Įsikirtimas", fm));
                    PdfPCell cell12 = new PdfPCell(new iTextSharp.text.Phrase("IV Įsikirtimas", fm));
                    PdfPCell cell13 = new PdfPCell(new iTextSharp.text.Phrase("V Įsikirtimas", fm));
                    PdfPCell cell14 = new PdfPCell(new iTextSharp.text.Phrase("VI Įsikirtimas", fm));
                    PdfPCell cell15 = new PdfPCell(new iTextSharp.text.Phrase("VII Įsikirtimas", fm));
                    PdfPCell cell16 = new PdfPCell(new iTextSharp.text.Phrase("VIII Įsikirtimas", fm));
                    PdfPCell cell17 = new PdfPCell(new iTextSharp.text.Phrase("IX Įsikirtimas", fm));
                    PdfPCell cell18 = new PdfPCell(new iTextSharp.text.Phrase("X Įsikirtimas", fm));
                    PdfPCell cell19 = new PdfPCell(new iTextSharp.text.Phrase("XI Įsikirtimas", fm));
                    PdfPCell cell20 = new PdfPCell(new iTextSharp.text.Phrase("XII Įsikirtimas", fm));
                    PdfPCell cell21 = new PdfPCell(new iTextSharp.text.Phrase("XIII Įsikirtimas", fm));
                    PdfPCell cell22 = new PdfPCell(new iTextSharp.text.Phrase("XIV Įsikirtimas", fm));
                    PdfPCell cell23 = new PdfPCell(new iTextSharp.text.Phrase("XV Įsikirtimas", fm));
                    PdfPCell cell24 = new PdfPCell(new iTextSharp.text.Phrase("Eteris 1", fm));
                    PdfPCell cell25 = new PdfPCell(new iTextSharp.text.Phrase("Eteris 2", fm));
                    PdfPCell cell26 = new PdfPCell(new iTextSharp.text.Phrase("Eteris 3", fm));
                    PdfPCell cell27 = new PdfPCell(new iTextSharp.text.Phrase("Eteris 4", fm));
                    PdfPCell cell28 = new PdfPCell(new iTextSharp.text.Phrase("Eteris 5", fm));
                    PdfPCell cell29 = new PdfPCell(new iTextSharp.text.Phrase("P raidė", fm));
                    PdfPCell cell30 = new PdfPCell(new iTextSharp.text.Phrase("Intarpai bėg#eil", fm));
                    PdfPCell cell31 = new PdfPCell(new iTextSharp.text.Phrase("Licencijos pradžia", fm));
                    PdfPCell cell32 = new PdfPCell(new iTextSharp.text.Phrase("Licencijos pabaiga", fm));
                    PdfPCell cell33 = new PdfPCell(new iTextSharp.text.Phrase("Žanras", fm));
                    PdfPCell cell34 = new PdfPCell(new iTextSharp.text.Phrase("Režisierius", fm));
                    PdfPCell cell35 = new PdfPCell(new iTextSharp.text.Phrase("Aktoriai", fm));
                    PdfPCell cell36 = new PdfPCell(new iTextSharp.text.Phrase("Šalis", fm));
                    PdfPCell cell37 = new PdfPCell(new iTextSharp.text.Phrase("Metai", fm));
                    PdfPCell cell38 = new PdfPCell(new iTextSharp.text.Phrase("Sezonas", fm));
                    PdfPCell cell39 = new PdfPCell(new iTextSharp.text.Phrase("Serija", fm));
                    PdfPCell cell40 = new PdfPCell(new iTextSharp.text.Phrase("Indeksas", fm));
                    //cell1.Border = 0;
                    //cell2.Border = 0;
                    //cell3.Border = 0;
                    //cell4.Border = 0;
                   // cell5.Border = 0;
                    //cell6.Border = 0;
                    table.AddCell(cell1);
                    table.AddCell(cell2);
                    table.AddCell(cell3);
                    table.AddCell(cell4);
                    table.AddCell(cell5);
                    table.AddCell(cell6);
                    table.AddCell(cell7);
                    table.AddCell(cell8);
                    table.AddCell(cell9);
                    table.AddCell(cell10);
                    table.AddCell(cell11);
                    table.AddCell(cell12);
                    table.AddCell(cell13);
                    table.AddCell(cell14);
                    table.AddCell(cell15);
                    table.AddCell(cell16);
                    table.AddCell(cell17);
                    table.AddCell(cell18);
                    table.AddCell(cell19);
                    table.AddCell(cell20);
                    table.AddCell(cell21);
                    table.AddCell(cell22);
                    table.AddCell(cell23);
                    table.AddCell(cell24);
                    table.AddCell(cell25);
                    table.AddCell(cell26);
                    table.AddCell(cell27);
                    table.AddCell(cell28);
                    table.AddCell(cell29);
                    table.AddCell(cell30);
                    table.AddCell(cell31);
                    table.AddCell(cell32);
                    table.AddCell(cell33);
                    table.AddCell(cell34);
                    table.AddCell(cell35);
                    table.AddCell(cell36);
                    table.AddCell(cell37);
                    table.AddCell(cell38);
                    table.AddCell(cell39);
                    table.AddCell(cell40);

                    for (int i = 0; i < dt.Items.Count - 1; i++)
                    {
                        DataRowView li = (DataRowView)dt.Items[i];
                        PdfPCell cell1_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(0).ToString(), fm));
                        PdfPCell cell2_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(1).ToString(), fm));
                        PdfPCell cell3_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(2).ToString(), fm));
                        PdfPCell cell4_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(3).ToString(), fm));
                        PdfPCell cell5_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(4).ToString(), fm));
                        PdfPCell cell6_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(5).ToString(), fm));
                        PdfPCell cell7_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(6).ToString(), fm));
                        PdfPCell cell8_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(7).ToString(), fm));
                        PdfPCell cell9_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(8).ToString(), fm));
                        PdfPCell cell10_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(9).ToString(), fm));
                        PdfPCell cell11_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(10).ToString(), fm));
                        PdfPCell cell12_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(11).ToString(), fm));
                        PdfPCell cell13_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(12).ToString(), fm));
                        PdfPCell cell14_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(13).ToString(), fm));
                        PdfPCell cell15_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(14).ToString(), fm));
                        PdfPCell cell16_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(15).ToString(), fm));
                        PdfPCell cell17_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(16).ToString(), fm));
                        PdfPCell cell18_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(17).ToString(), fm));
                        PdfPCell cell19_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(18).ToString(), fm));
                        PdfPCell cell20_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(19).ToString(), fm));
                        PdfPCell cell21_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(20).ToString(), fm));
                        PdfPCell cell22_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(21).ToString(), fm));
                        PdfPCell cell23_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(22).ToString(), fm));
                        PdfPCell cell24_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(23).ToString(), fm));
                        PdfPCell cell25_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(24).ToString(), fm));
                        PdfPCell cell26_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(25).ToString(), fm));
                        PdfPCell cell27_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(26).ToString(), fm));
                        PdfPCell cell28_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(27).ToString(), fm));
                        PdfPCell cell29_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(28).ToString(), fm));
                        PdfPCell cell30_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(29).ToString(), fm));
                        PdfPCell cell31_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(30).ToString(), fm));
                        PdfPCell cell32_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(31).ToString(), fm));
                        PdfPCell cell33_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(32).ToString(), fm));
                        PdfPCell cell34_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(33).ToString(), fm));
                        PdfPCell cell35_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(35).ToString(), fm));
                        PdfPCell cell36_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(36).ToString(), fm));
                        PdfPCell cell37_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(37).ToString(), fm));
                        PdfPCell cell38_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(38).ToString(), fm));
                        PdfPCell cell39_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(39).ToString(), fm));
                        PdfPCell cell40_ = new PdfPCell(new iTextSharp.text.Phrase(li.Row.ItemArray.GetValue(40).ToString(), fm));
                
                        table.AddCell(cell1_);
                        table.AddCell(cell2_);
                        table.AddCell(cell3_);
                        table.AddCell(cell4_);
                        table.AddCell(cell5_);
                        table.AddCell(cell6_);
                        table.AddCell(cell7_);
                        table.AddCell(cell8_);
                        table.AddCell(cell9_);
                        table.AddCell(cell10_);
                        table.AddCell(cell11_);
                        table.AddCell(cell12_);
                        table.AddCell(cell13_);
                        table.AddCell(cell14_);
                        table.AddCell(cell15_);
                        table.AddCell(cell16_);
                        table.AddCell(cell17_);
                        table.AddCell(cell18_);
                        table.AddCell(cell19_);
                        table.AddCell(cell20_);
                        table.AddCell(cell21_);
                        table.AddCell(cell22_);
                        table.AddCell(cell23_);
                        table.AddCell(cell24_);
                        table.AddCell(cell25_);
                        table.AddCell(cell26_);
                        table.AddCell(cell27_);
                        table.AddCell(cell28_);
                        table.AddCell(cell29_);
                        table.AddCell(cell30_);
                        table.AddCell(cell31_);
                        table.AddCell(cell32_);
                        table.AddCell(cell33_);
                        table.AddCell(cell34_);
                        table.AddCell(cell35_);
                        table.AddCell(cell36_);
                        table.AddCell(cell37_);
                        table.AddCell(cell38_);
                        table.AddCell(cell39_);
                        table.AddCell(cell40_);
                    }

                    doc.Add(table); 
                    

                   //----------------end header----------

                 /*   string sql = "SELECT  Agenda.SortOrder as so,Agenda.ItemNumber as itemnr,max(VotingSession.Version) as vr,VotingSession.Id as id, AgendaText.Agenda_id as aid, case when REPLACE(REPLACE(REPLACE(REPLACE(Description,CHAR(9),''),CHAR(10),''),CHAR(13),''),CHAR(32),'') = '' then Name else case when Description IS NULL then Name else Description end end as FullName, case when Quorum = '0' then 'NĖRA KVORUMO' else case when Passed = '1' then 'PRIIMTAS' else 'NEPRIIMTAS' end end as VoteRes, 'UŽ '+CAST(" + Configuration.rd_uz + " as varchar) as res1, 'SUSILAIKĖ '+CAST(" + Configuration.rd_pries + " as varchar) as res3, 'PRIEŠ '+CAST(" + Configuration.rd_susilaike + " as varchar) as res2, 'NEBALSAVO   '+CAST(VotingSession.Result5 as varchar) as res5, 'Užsiregistravo '+CAST(VotingSession.Result6 as varchar) as res6 from AgendaText left join VotingSession on VotingSession.Agenda_id=AgendaText.Agenda_id left join Agenda on Agenda.Id=AgendaText.Agenda_id where AgendaText.Agenda_id in (Select ID from Agenda where Conference_id=" + guid + " ) group by Agenda.SortOrder,Agenda.ItemNumber,AgendaText.Agenda_id, AgendaText.Name, AgendaText.Description,VotingSession.Passed,VotingSession.Quorum,VotingSession.Result1,VotingSession.Result2,VotingSession.Result3,VotingSession.Result5,VotingSession.Result6,VotingSession.Id order by VotingSession.Id asc";
                    DataTable tbagenda = db.getTable(sql);
                    int nr = 0;
                    if (tbagenda.Rows.Count > 0)
                    {
                        for (int i = 0; i < tbagenda.Rows.Count; i++)
                        {

                            string paragraph = "";
                            string itemnumber = "";
                            try
                            {
                                itemnumber = tbagenda.Rows[i]["itemnr"].ToString();
                            }
                            catch (Exception ex) { }
                            string version = tbagenda.Rows[i]["vr"].ToString();
                            string name = tbagenda.Rows[i]["FullName"].ToString();
                            string status = tbagenda.Rows[i]["VoteRes"].ToString();
                            string res1 = tbagenda.Rows[i]["res1"].ToString();
                            string res2 = tbagenda.Rows[i]["res2"].ToString();
                            string res3 = tbagenda.Rows[i]["res3"].ToString();
                            string res5 = tbagenda.Rows[i]["res5"].ToString();
                            string res6 = tbagenda.Rows[i]["res6"].ToString();
                            string id = tbagenda.Rows[i]["id"].ToString();
                            string aid = tbagenda.Rows[i]["aid"].ToString();
                            string aid_next = "0";
                            try
                            {
                                aid_next = tbagenda.Rows[i + 1]["aid"].ToString();
                            }
                            catch (Exception ex) { aid_next = "0"; }
                            string sqlvs = "select * from VotingSession where conference_id=" + guid + " and id=" + id;
                            DataTable tbvotingsession = db.getTable(sqlvs);
                            if (tbvotingsession.Rows.Count > 0)
                            {

                                if (!aid.Equals(aid_next))
                                {
                                    string sqlres = "select distinct LogDelegate.delegate_id as subid,DelegateText.name,case when VotingResult.Vote1=1 then 'Už' else case when VotingResult.Vote3=1 then 'Susilaikė' else case when VotingResult.Vote2=1 then 'Prieš' end end end as vt from LogDelegate left join DelegateText on LogDelegate.delegate_id=DelegateText.delegate_id left join VotingResult on  VotingResult.LogDelegate_id=DelegateText.delegate_id left join VotingSession on VotingResult.VotingSession_id=VotingSession.Id where LogDelegate.conference_id=" + guid + " and VotingSession.id=" + id + " ORDER BY DelegateText.name ASC";
                                    DataTable tbres = db.getTable(sqlres);

                                    iTextSharp.text.Chunk ch = new iTextSharp.text.Chunk();
                                    ch.Font = fLTB;

                                    string votingparameter_id = tbvotingsession.Rows[0]["VotingParameter_id"].ToString();
                                    if (votingparameter_id.Equals("69"))
                                    {
                                        ch.Append("SLAPTO BALSAVIMO REZULTATŲ PROTOKOLAS");

                                    }
                                    else
                                    {
                                        ch.Append("ATVIRO BALSAVIMO REZULTATŲ PROTOKOLAS");
                                    }
                                    p2.Clear();
                                    p2.Add(ch);
                                    doc.Add(p2);
                                    doc.Add(p1);

                                    nr++;
                                    iTextSharp.text.Chunk cnn = new iTextSharp.text.Chunk(name.Replace('\r', ' ').Replace('\n', ' ').Replace('\t', ' '), f);
                                    p2.Clear();
                                    p2.Add(cnn);
                                    doc.Add(p2);

                                    PdfPTable table = new PdfPTable(2);
                                    table.DefaultCell.Border = 0;
                                    table.WidthPercentage = 80f;

                                    string nr_meet = "";
                                    string countdel = "";
                                

                                     string col1row1 = nr_meet;
                                    string col2row1 = "";
                                    string col1row2 = "";
                                    if (tbvotingsession.Rows.Count > 0)
                                    {
                                        col2row1 = "Užsiregistravo      " + tbvotingsession.Rows[0]["TotalLoggedIn"].ToString();
                                        col1row2 = tbvotingsession.Rows[0]["Date"].ToString().Substring(0, 19);
                                    }
                                    else
                                    {
                                        col2row1 = "Užsiregistravo      0";
                                        col1row2 = "";
                                    }
                                    string col2row2 = "Viso balsavo        ";
                                    if (tbvotingsession.Rows.Count > 0)
                                    {
                                        col2row2 = col2row2 + tbvotingsession.Rows[0]["Result5"].ToString(); //col2row2 = col2row2 + tbvotingsession.Rows[0]["TotalVotes"].ToString();
                                    }
                                    else
                                    {
                                        col2row2 = col2row2 + "0";
                                    }
                                    string col1row3 = "Sąraše Tarybos narių: " + countdel;
                                    string col2row3 = res1 + "    " + res2 + "    " + res3.Replace("SUSILAIKE", "SUSILAIKĖ");
                                    PdfPCell cell1 = new PdfPCell(new iTextSharp.text.Phrase(col1row1, fm));
                                    PdfPCell cell2 = new PdfPCell(new iTextSharp.text.Phrase(col2row1, fm));
                                    PdfPCell cell3 = new PdfPCell(new iTextSharp.text.Phrase(col1row2, fm));
                                    PdfPCell cell4 = new PdfPCell(new iTextSharp.text.Phrase(col2row2, fm));
                                    PdfPCell cell5 = new PdfPCell(new iTextSharp.text.Phrase(col1row3, fm));
                                    PdfPCell cell6 = new PdfPCell(new iTextSharp.text.Phrase(col2row3, fm));
                                    cell1.Border = 0;
                                    cell2.Border = 0;
                                    cell3.Border = 0;
                                    cell4.Border = 0;
                                    cell5.Border = 0;
                                    cell6.Border = 0;
                                    table.AddCell(cell1);
                                    table.AddCell(cell2);
                                    table.AddCell(cell3);
                                    table.AddCell(cell4);
                                    table.AddCell(cell5);
                                    if (name.ToUpper().Contains("KVORUMO PATIKRINIMAS"))
                                    {
                                        table.AddCell("");
                                    }
                                    else
                                    {
                                        table.AddCell(cell6);
                                    }

                                    doc.Add(table);

                                    doc.Add(p1);

                                 
                                    PdfPTable tableres = new PdfPTable(3);
                                    tableres.DefaultCell.Border = 0;
                                    tableres.WidthPercentage = 80f;

                                    string sqlres1 = "select distinct LogDelegate.delegate_id as subid,DelegateText.name,case when " + Configuration.rd_uz.Replace("VotingSession.Result", "VotingResult.Vote") + "=1 then 'UŽ' else case when " + Configuration.rd_susilaike.Replace("VotingSession.Result", "VotingResult.Vote") + "=1 then 'SUSILAIKĖ' else case when " + Configuration.rd_pries.Replace("VotingSession.Result", "VotingResult.Vote") + "=1 then 'PRIEŠ' end end end as vt from LogDelegate left join DelegateText on LogDelegate.delegate_id=DelegateText.delegate_id left join VotingResult on  VotingResult.LogDelegate_id=DelegateText.delegate_id left join VotingSession on VotingResult.VotingSession_id=VotingSession.Id where LogDelegate.conference_id=" + guid + " and VotingSession.id=" + id + " ORDER BY DelegateText.name ASC";
                                    DataTable tbres1 = db.getTable(sqlres1);

                                    if (name.ToUpper().Contains("KVORUMO PATIKRINIMAS"))
                                    {
                                        iTextSharp.text.Chunk cnr = new iTextSharp.text.Chunk("REGISTRAVOSI", f);
                                        p2.Clear();
                                        p2.Add(cnr);
                                        doc.Add(p2);
                                        doc.Add(p1);
                                    }

                                    if (tbres1.Rows.Count > 0)
                                    {
                                        int yy = 0;
                                        for (int y = 0; y < tbres1.Rows.Count; y++)
                                        {

                                            iTextSharp.text.Paragraph pt = new iTextSharp.text.Paragraph();
                                            pt.Alignment = iTextSharp.text.Element.ALIGN_LEFT;
                                            if (name.ToUpper().Contains("KVORUMO PATIKRINIMAS"))
                                            {
                                                if (tbres1.Rows[y]["vt"].ToString().Contains("UŽ"))
                                                {
                                                    yy++;
                                                    PdfPCell cl1 = new PdfPCell(new iTextSharp.text.Phrase((yy).ToString(), fm));//tbres.Rows[y]["Nr"].ToString()));
                                                    cl1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT;
                                                    cl1.PaddingRight = 10f;
                                                    cl1.Border = 0;
                                                    tableres.AddCell(cl1);

                                                    iTextSharp.text.Chunk cbt = new iTextSharp.text.Chunk(tbres1.Rows[y]["name"].ToString(), fm);
                                                    pt.Clear();
                                                    pt.Add(cbt);
                                                    tableres.AddCell(pt);

                                                    PdfPCell cl2 = new PdfPCell(new iTextSharp.text.Phrase("REGISTRAVOSI", fm));
                                                    cl2 = new PdfPCell(new iTextSharp.text.Phrase("", fm));
                                                    cl2.Border = 0;
                                                    tableres.AddCell(cl2);
                                                }
                                            }
                                            else
                                            {
                                                PdfPCell cl1 = new PdfPCell(new iTextSharp.text.Phrase((y + 1).ToString(), fm));//tbres.Rows[y]["Nr"].ToString()));
                                                cl1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT;
                                                cl1.PaddingRight = 10f;
                                                cl1.Border = 0;
                                                tableres.AddCell(cl1);

                                                iTextSharp.text.Chunk cbt = new iTextSharp.text.Chunk(tbres1.Rows[y]["name"].ToString(), fm);
                                                pt.Clear();
                                                pt.Add(cbt);
                                                tableres.AddCell(pt);

                                                PdfPCell cl2 = new PdfPCell(new iTextSharp.text.Phrase("REGISTRAVOSI", fm));
                                                cl2 = new PdfPCell(new iTextSharp.text.Phrase(tbres1.Rows[y]["vt"].ToString().Replace("SUSILAIKE", "SUSILAIKĖ"), fm));
                                                cl2.Border = 0;
                                                tableres.AddCell(cl2);
                                            }

                                        }
                                    }

                                    p2.Clear();
                                    p2.Add(tableres);
                                    doc.Add(p2);
                                    doc.Add(p1);
                         
                                    doc.NewPage();
                                }
                            
                        
                    }

                }
            }*/
            doc.Close();

        }

    /*    protected static int getTop(pdfPage myPage, int nTop)
        {
            return myPage.height - nTop;
        }

        public static Bitmap getPagePDF(System.Windows.Forms.PictureBox pc, string filename, int page)
        {
            Bitmap _backbuffer = null;
            try
            {
                PDFWrapper _pdfDoc = new PDFWrapper();
                _pdfDoc.LoadPDF(filename);
                _pdfDoc.CurrentPage = page;
                _pdfDoc.CurrentY = 0;
                _pdfDoc.CurrentX = 0;
                pdfPages = _pdfDoc.PageCount;

                _pdfDoc.FitToWidth(pc.Handle);
                _pdfDoc.RenderPage(pc.Handle);

                _backbuffer = new Bitmap(_pdfDoc.PageWidth, _pdfDoc.PageHeight);
                _pdfDoc.ClientBounds = new Rectangle(0, 0, _pdfDoc.PageWidth, _pdfDoc.PageHeight);
                //using (
                Graphics g = Graphics.FromImage(_backbuffer);//)
                // {
                _pdfDoc.DrawPageHDC(g.GetHdc());
                g.ReleaseHdc();
                g.Dispose();
                g = null;
                // }
                _pdfDoc.Dispose();
                _pdfDoc = null;
            }
            catch (Exception ex) { _backbuffer = null; }
            return _backbuffer;
        }

        public static Bitmap printPagePDF(string filename, int page)
        {
            int pageWidth = 827;
            int pageHeight = 1169;
            System.Windows.Forms.PictureBox pc = new System.Windows.Forms.PictureBox();
            pc.Width = pageWidth;
            pc.Height = pageHeight;
            PDFWrapper _pdfDoc = new PDFWrapper();
            _pdfDoc.LoadPDF(filename);
            _pdfDoc.CurrentPage = page;
            _pdfDoc.CurrentY = 0;
            _pdfDoc.CurrentX = 0;
            pdfPages = _pdfDoc.PageCount;

            _pdfDoc.FitToWidth(pc.Handle);
            _pdfDoc.RenderPage(pc.Handle);

            Bitmap _backbuffer = new Bitmap(_pdfDoc.PageWidth, _pdfDoc.PageHeight);
            _pdfDoc.ClientBounds = new Rectangle(0, 0, _pdfDoc.PageWidth, _pdfDoc.PageHeight);
            using (Graphics g = Graphics.FromImage(_backbuffer))
            {
                _pdfDoc.DrawPageHDC(g.GetHdc());
                g.ReleaseHdc();
            }
            return _backbuffer;
        }


        public static void saveFilePDF(string name, string newname)
        {
            try
            {
                File.Copy(name, newname, true);
            }
            catch (Exception ex) { File.Copy(name, newname, true); }
        }  */

  
        public static void editRegistry()
        {
            Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\Shell\\AttachmentExecute");
            Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\Shell\\AttachmentExecute\\{0002DF01-0000-0000-C000-000000000046}");
            Registry.CurrentUser.Flush();
            RegistryKey myKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\Shell\\AttachmentExecute\\{0002DF01-0000-0000-C000-000000000046}", true);
            object value = 0;
            myKey.SetValue("Excel.Sheet.8", value);
            // Registry.SetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\AttachmentExecute\\{0002DF01-0000-0000-C000-000000000046}", "Excel.Sheet.8", "0",RegistryValueKind.QWord);
            Registry.CurrentUser.Flush();

            myKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Classes\\Excel.Sheet.8", true);
            myKey.SetValue("BrowserFlags", value, RegistryValueKind.DWord);
            Registry.LocalMachine.Flush();

        }

      
        public static void FindAndKillProcess(string name)
        {
            foreach (Process clsProcess in Process.GetProcesses())
            {
                if (clsProcess.ProcessName.Contains(name))
                {
                    clsProcess.Kill();
                }
            }
        }

       
        public static void addPageNumber()
        {
            FileInfo f = new FileInfo(AppPath + "\\temp.pdf");
            byte[] pdf = WriteToPdf(f, "Psl. {0} iš {1}");
            FileStream fileStream = new FileStream(AppPath + "\\temp.pdf", FileMode.OpenOrCreate);
            fileStream.Write(pdf, 0, pdf.Length);
            fileStream.Close();

        }

        public static byte[] WriteToPdf(FileInfo sourceFile, string stringToWriteToPdf)
        {
            PdfReader reader = new PdfReader(sourceFile.FullName);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                PdfStamper pdfStamper = new PdfStamper(reader, memoryStream);

                for (int i = 1; i <= reader.NumberOfPages; i++) // Must start at 1 because 0 is not an actual page.
                {
                    //
                    // If you ask for the page size with the method getPageSize(), you always get a
                    // Rectangle object without rotation (rot. 0 degrees)—in other words, the paper size
                    // without orientation. That’s fine if that’s what you’re expecting; but if you reuse
                    // the page, you need to know its orientation. You can ask for it separately with
                    // getPageRotation(), or you can use getPageSizeWithRotation(). - (Manning Java iText Book)
                    //   
                    //
                    iTextSharp.text.Rectangle pageSize = reader.GetPageSizeWithRotation(i);

                    //
                    // Gets the content ABOVE the PDF, Another option is GetUnderContent(...)  
                    // which will place the text below the PDF content. 
                    //
                    PdfContentByte pdfPageContents = pdfStamper.GetUnderContent(i);
                    pdfPageContents.BeginText(); // Start working with text.

                    //
                    // Create a font to work with 
                    //
                    // BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName, false);
                    BaseFont baseFont = BaseFont.CreateFont("c:\\windows\\fonts\\arial.ttf", "windows-1257", true);
                    pdfPageContents.SetFontAndSize(baseFont, 10); // 40 point font
                    pdfPageContents.SetRGBColorFill(0, 0, 0); // Sets the color of the font, RED in this instance


                    //
                    // Angle of the text. This will give us the angle so we can angle the text diagonally 
                    // from the bottom left corner to the top right corner through the use of simple trigonometry. 
                    //
                    float textAngle = 0;

                    //
                    // Note: The x,y of the Pdf Matrix is from bottom left corner. 
                    // This command tells iTextSharp to write the text at a certain location with a certain angle.
                    // Again, this will angle the text from bottom left corner to top right corner and it will 
                    // place the text in the middle of the page. 
                    //
                    string strpsl = String.Format(stringToWriteToPdf, i, reader.NumberOfPages);
                    pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, strpsl,
                                                    pageSize.Width - 50,
                                                    25,
                                                    textAngle);

                    pdfPageContents.EndText(); // Done working with text
                }
                pdfStamper.FormFlattening = true; // enable this if you want the PDF flattened. 
                pdfStamper.Close(); // Always close the stamper or you'll have a 0 byte stream. 


                return memoryStream.ToArray();
            }
        }

       

        public static void cleanUp()
        {
            Process[] prs = Process.GetProcesses();
            foreach (Process pr in prs)
            {

                if ((pr.ProcessName.Contains("WINWORD")) || (pr.ProcessName.Contains("EXCEL")))
                {
                    pr.Kill();
                }
            }
        }

       
        
    }

    public class ParamObject
    {
        public string param1 { get; set; }
        public string tmp { get; set; }
        public string meeting_title { get; set; }
        public string guid { get; set; }
    }


}
